﻿#include<iostream>
#include<fstream>
#include"bSpline.h"
#include "Function.h"

using namespace std;

class FucntionC : public Function
{
public:
	double operator()(const double x) const {
		return 1 / (1 + x * x);
	}
	double diff(const double x) const {
		return -2 * x / pow(1 + x * x, 2);
	}
	double diff2(const double x) const {
		return (6 * x * x - 2) / pow(1 + x * x, 3);
	}

} functionC;

int main()
{
	vector<double> knots;
	for (int i = 1; i <= 11; i++)
	{
		knots.push_back(i - 6);
	}
	vector<double> values1;
	for (int i = 0; i < knots.size(); i++)
	{
		values1.push_back(functionC(knots[i]));
	}

	Bform s2(functionC, knots, values1, 3, 2);
	ofstream f1;
	f1.open("./Data/result_C_cubic.txt", ios::out);
	for (int i = 0; i < 2001; i++)
	{
		double temp = -5 + 1.0 * i * 10.0 / 2000;
		f1 << temp << " " << s2.cubic(temp) << "\n ";
	}
	f1 << endl;


	ofstream f2;
	f2.open("./Data/result_C_quad.txt", ios::out);
	vector<double> values2;
	values2.push_back(1.0 / 26);
	for (int i = 1; i <= 10; i++)
	{
		values2.push_back(functionC(i - 5.5));
	}
	values2.push_back(1.0 / 26);
	Bform s3(knots, values2, 2);

	for (int i = 0; i < 2001; i++)
	{
		double temp = -5 + 1.0 * i * 10.0 / 2000;
		f2 << temp << " " << s3.quad(temp) << "\n ";
	}
	f2 << endl;

	f2.close();

	ofstream f3;
	f3.open("./Data/result_C_error.txt", ios::out);
	vector<double> xsites{ -3.5,-3, -0.5, 0, 0.5, 3, 3.5 };
	for (int i = 0; i < xsites.size(); i++) {
		double index = xsites[i];
		f3 << "x =" << index << ":" << endl;
		f3 << "cubic: " << abs(s2.cubic(index) - functionC(index));
		f3 << "                                            quad : " << abs(s3.quad(index) - functionC(index)) << endl;
		f3 << endl;
	}
	







	return 0;
}
